cmake_minimum_required(VERSION 3.12)
project(yolov8_openvino_inference)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -O3 -march=native -mtune=native")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -march=native -mtune=native -flto")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")

# Enable Link Time Optimization
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)

# Find required packages
find_package(OpenCV REQUIRED)
find_package(nlohmann_json 3.2.0 QUIET)

# Try to find OpenVINO
find_package(OpenVINO QUIET)

message(STATUS "OpenVINO_FOUND: ${OpenVINO_FOUND}")

# Always try to find OpenVINO manually as fallback
# Try multiple possible locations
if(EXISTS "/opt/intel/openvino_genai_ubuntu24_2025.3.0.0_x86_64/runtime/include")
	set(OPENVINO_INCLUDE_DIR "/opt/intel/openvino_genai_ubuntu24_2025.3.0.0_x86_64/runtime/include")
	set(OPENVINO_LIB_DIR "/opt/intel/openvino_genai_ubuntu24_2025.3.0.0_x86_64/runtime/lib/intel64")
	message(STATUS "Using OpenVINO from: /opt/intel/openvino_genai_ubuntu24_2025.3.0.0_x86_64")
elseif(EXISTS "/opt/intel/openvino/runtime/include")
	set(OPENVINO_INCLUDE_DIR "/opt/intel/openvino/runtime/include")
	set(OPENVINO_LIB_DIR "/opt/intel/openvino/runtime/lib/intel64")
	message(STATUS "Using OpenVINO from: /opt/intel/openvino")
else()
	# Default fallback
	set(OPENVINO_INCLUDE_DIR "/opt/intel/openvino/runtime/include")
	set(OPENVINO_LIB_DIR "/opt/intel/openvino/runtime/lib/intel64")
	message(STATUS "Using default OpenVINO path")
endif()

message(STATUS "OpenVINO Include Dir: ${OPENVINO_INCLUDE_DIR}")
message(STATUS "OpenVINO Lib Dir: ${OPENVINO_LIB_DIR}")

# Include directories
include_directories(
	${OpenCV_INCLUDE_DIRS}
	${OPENVINO_INCLUDE_DIR}
)

# Link directories
link_directories(${OPENVINO_LIB_DIR})

# Main executable for visualization (original)
add_executable(detect
	src/main.cc
	src/inference.cc
)

target_link_libraries(detect
	${OpenCV_LIBS}
	openvino
)

# Service executable for Go integration
add_executable(detect_service
	src/main_service.cc
	src/inference.cc
)

target_link_libraries(detect_service
	${OpenCV_LIBS}
	openvino
)

# Installation
install(TARGETS detect detect_service DESTINATION bin)
